﻿using DotNet.Utilities;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JinfuyuanProject.Controllers
{
    public class SalaryController : Controller
    {
        private readonly ISalaryService _salaryService;
        private readonly SalaryFilter _filter;
        private readonly ISalaryDetailsService _salaryDetailService;

        public SalaryController(ISalaryService salaryService, SalaryFilter filter, ISalaryDetailsService salaryDetailsService)
        {
            _salaryService = salaryService;
            _filter = filter;
            _salaryDetailService = salaryDetailsService;
        }

        #region 页面
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Salary()
        {
            return View();
        }

        public ActionResult SalaryDetail(int s_id)
        {
            ViewBag.s_id = s_id;
            var salary = _salaryService.GetModel(s_id);
            if (salary != null)
            {
                ViewBag.BaseSalary = salary.BaseSalary;
                ViewBag.ExtraSalary = salary.ExtraSaraly;
                ViewBag.TotalSalary = salary.TotalSaraly;
            }
            else
            {
                ViewBag.Name = "未知";
                ViewBag.Numbers = "未知";
                ViewBag.BaseSalary = 0;
                ViewBag.ExtraSalary = 0;
                ViewBag.TotalSalary = 0;
            }
            return View();
        }
        #endregion

        #region 接口
        public JsonResult DataList(int page, int limit, string d_id)
        {
            _filter.page = page;
            _filter.limit = limit;
            _filter.d_id = d_id;
            ResMessage res = _salaryService.GetSalaryList(_filter);
            res.code = 0;
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        public JsonResult SalaryDetailList(int page, int limit, int s_id)
        {
            _filter.page = page;
            _filter.limit = limit;
            ResMessage res = _salaryService.GetSalaryDetailsList(_filter, s_id);
            res.code = 0;
            return Json(res, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult ExportExcel()
        {
            try
            {
                // 调用服务层生成Excel
                var result = _salaryService.GenerateSalaryExcel();

                if ((int)result["code"] == 0)
                {
                    
                    // 备份文件所在路径

                    string filePath = result["path"].ToString();
                    string sheetName = result["sheetName"].ToString();
                    // 确保文件存在
                    if (System.IO.File.Exists(filePath))
                    {
                        // 返回文件内容
                        return File(filePath, "application/octet-stream", sheetName);
                    }
                    else
                    {
                        return HttpNotFound("File not found");
                    }
                }
                else
                {
                    return Json(new
                    {
                        code = -1,
                        message = result["message"]
                    });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    code = -1,
                    message = $"导出Excel失败：{ex.Message}"
                }, JsonRequestBehavior.AllowGet);
            }
        }
        public FileResult ServiceDetail()
        {
            var result = _salaryService.GenerateSalaryExcel();
            // 3. 读取文件并返回下载
            string filePath = result["path"]?.ToString();
            byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
            return File(
                fileBytes,
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                $"员工薪资{DateTime.Now.ToString("yyyyMMdd")}.xlsx"
            );
        }
        // 提供文件下载
        public ActionResult DownloadExcel(string filePath)
        {
            if (!System.IO.File.Exists(filePath))
            {
                return Content("文件不存在");
            }

            return File(filePath, "application/vnd.ms-excel", Path.GetFileName(filePath));
        }
        #endregion
    }
}